const common = require('./base/common');

/**
 * 字节跳动(2021/3/2)一面算法题：最长不重复子序列
 */
let maxLengthSubSeq = function(str) {
    let left = 0, set = new Set(), result = "", max = 0;

    let chars = str.split("");
    for(let right=0; right < chars.length;) {
        if(set.has(chars[right])) {
            set.delete(chars[left]);
            left++;
        } else {
            set.add(chars[right]);
            right++;

            if(set.size > max) {
                max = set.size;
                result = str.substring(left, right);
            }
        }
    }

    return result;
}

common.testing([
    "faduafadfuoerqvnaqrujvvnal",
    "aaaaaaabbbbbbbaaaaaaaaaaa"
])((x) => maxLengthSubSeq(x));
